package cn.iocoder.yudao.module.erp.dal.mysql.purchase;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.ErpSupplierPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.supplier.ErpSupplierRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * ERP 供应商 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ErpSupplierMapper extends BaseMapperX<ErpSupplierDO> {

    default PageResult<ErpSupplierDO> selectPage(ErpSupplierPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpSupplierDO>()
                .eqIfPresent(ErpSupplierDO::getId, reqVO.getId())
                .likeIfPresent(ErpSupplierDO::getName, reqVO.getName())
                .likeIfPresent(ErpSupplierDO::getMobile, reqVO.getMobile())
                .likeIfPresent(ErpSupplierDO::getTelephone, reqVO.getTelephone())
                .orderByDesc(ErpSupplierDO::getId,ErpSupplierDO::getSort));
    }

    default PageResult<ErpSupplierDO> selectRegisterPage(ErpSupplierPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpSupplierDO>()
                .eqIfPresent(ErpSupplierDO::getId, reqVO.getId())
                .eqIfPresent(ErpSupplierDO::getName, reqVO.getName())
                .eqIfPresent(ErpSupplierDO::getMobile, reqVO.getMobile())
                .eqIfPresent(ErpSupplierDO::getContact, reqVO.getContact())
                .eqIfPresent(ErpSupplierDO::getTelephone, reqVO.getTelephone())
                .orderByDesc(ErpSupplierDO::getId,ErpSupplierDO::getSort));
    }


    default PageResult<ErpSupplierDO> selectRegisterVerifyPage(ErpSupplierPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpSupplierDO>()
                .eqIfPresent(ErpSupplierDO::getId, reqVO.getId())
                .likeIfPresent(ErpSupplierDO::getName, reqVO.getName())
                .eqIfPresent(ErpSupplierDO::getMobile, reqVO.getMobile())
                .eqIfPresent(ErpSupplierDO::getContact, reqVO.getContact())
                .eqIfPresent(ErpSupplierDO::getTelephone, reqVO.getTelephone())
                .orderByDesc(ErpSupplierDO::getStatus,ErpSupplierDO::getName));
    }

    List<ErpSupplierDO> selectPageList (ErpSupplierPageReqVO reqVO);

    int total (ErpSupplierPageReqVO reqVO);

    default List<ErpSupplierDO> selectListByStatus(Integer status) {
        return selectList(ErpSupplierDO::getStatus, status);
    }

    /**
     * 产品名称筛选框查询方法
     * @param status
     * @param like
     * @return
     */
    @Select("select distinct id,name,full_pinyin,first_pinyin,wubi_shortcode from erp_supplier where deleted=0 and status =#{status} and (name like #{like} or full_pinyin like #{like}  or wubi_shortcode like #{like}  or first_pinyin like #{like} ) limit 50;")
    List<ErpProductRespVO> selectLists(Integer status, String like);

    default ErpSupplierDO selectById(Long id) {
        return selectOne(ErpSupplierDO::getId, id);
    }

    default int deleteById(Long id) {
        return delete(ErpSupplierDO::getId, id);
    }

    @Select(" select sort,min_order_quantity,supplier_id from erp_relationship where product_id = #{id}")
    List<ErpSupplierRespVO> selectRel(Long id);

    default List<ErpSupplierDO> selectListByIds(List<Long> ids){
        return selectList(ErpSupplierDO::getId,ids);
    }

}